package com.gomore.opple.module.incomeandexpense;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioGroup;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.module.BaseFragment;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.module.incomeandexpense.adapter.ApplyProgressAdapter;
import com.gomore.opple.module.incomeandexpense.adapter.InComeAndExpenseAdapter;
import com.gomore.opple.module.main.home.adapter.ConsumerAdapter;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.widgets.PullBaseView;
import com.gomore.opple.widgets.PullRecyclerView;
import com.gomore.opple.widgets.adapter.MultiItemTypeAdapter;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * Created by robot
 */
public class IncomeAndExpenseFragment extends BaseFragment implements IncomeAndExpenseContract.View,
        PullBaseView.OnRefreshListener {

    @Bind(R.id.recyclerView)
    PullRecyclerView recyclerView;
    @Bind(R.id.empty_view)
    View empty_view;
    @Bind(R.id.radio_group)
    RadioGroup radio_group;
    private String type = GlobalConstant.reflashType.INCOMEANDEXPENSE;

    private IncomeAndExpenseContract.Presenter mPresenter;

    public static IncomeAndExpenseFragment getInstance() {
        return new IncomeAndExpenseFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_income_and_expense;
    }

    @Override
    protected void initalizeData() {
        mPresenter.queryIncomeAndExpense(false, true);
    }

    @Override
    protected void initalizeViews() {
        recyclerView.setOnRefreshListener(this);
        showIncomeAndExpense();
        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.left_radio:
                        type = GlobalConstant.reflashType.INCOMEANDEXPENSE;
                        showIncomeAndExpense();
                        break;
                    case R.id.right_radio:
                        type = GlobalConstant.reflashType.APPLY;
                        showApply();
                        break;
                }
            }
        });
    }


    @Override
    public void setPresenter(IncomeAndExpenseContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    /**
     * 加载完毕
     */
    @Override
    public void showLoadMoreCompleted() {
        recyclerView.onFooterRefreshComplete();
    }

    /**
     * 刷新完毕
     */
    @Override
    public void showRefreshCompleted() {
        recyclerView.onHeaderRefreshComplete();
    }

    @Override
    public void showIncomeAndExpense() {
        InComeAndExpenseAdapter inComeAndExpenseAdapter = new InComeAndExpenseAdapter(getActivity(),
                R.layout.income_and_expense_item, mPresenter.getIncomeAndExpenseData());
        recyclerView.setLayoutManager(new LinearLayoutManager(IncomeAndExpenseFragment.this.getActivity()));
        recyclerView.setAdapter(inComeAndExpenseAdapter);
        inComeAndExpenseAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                IntentStart.getInstance().startIncomeAndExpenseDetailActivity(getActivity(), mPresenter.getIncomeAndExpenseData().get(position));
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        if (mPresenter.getIncomeAndExpenseData().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            empty_view.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            empty_view.setVisibility(View.GONE);
        }
    }

    @Override
    public void showApply() {
        ApplyProgressAdapter applyProgressAdapter = new ApplyProgressAdapter(getActivity(),
                R.layout.apply_item, mPresenter.getApplyData());
        recyclerView.setLayoutManager(new LinearLayoutManager(IncomeAndExpenseFragment.this.getActivity()));
        recyclerView.setAdapter(applyProgressAdapter);
        applyProgressAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                IntentStart.getInstance().startApplyDetailActivity(getActivity(), mPresenter.getApplyData().get(position));
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        if (mPresenter.getApplyData().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            empty_view.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            empty_view.setVisibility(View.GONE);
        }
    }

    @Override
    public void onHeaderRefresh(PullBaseView view) {
        if (type.equals(GlobalConstant.reflashType.INCOMEANDEXPENSE)) {
            mPresenter.queryIncomeAndExpense(false, false);
        } else {
            mPresenter.queryApply(false, false);
        }
    }

    @Override
    public void onFooterRefresh(PullBaseView view) {
        if (type.equals(GlobalConstant.reflashType.INCOMEANDEXPENSE)) {
            mPresenter.queryIncomeAndExpense(true, false);
        } else {
            mPresenter.queryApply(true, false);
        }
    }
}
